<?php

namespace Platform\Services\User;

use Platform\Models\User\User;
use Platform\Models\User\OpenUnion;
use Platform\Services\BaseService;

class UserService extends BaseService {

    public function OpenIdLogin($openId, $type) {
        $openUnionModel = model('User/OpenUnion');
        $openUnionInfo = $openUnionModel->getDataByOpenId($openId, $type);
        if (empty($openUnionInfo)) {
            return false;
        }
        $userMdeol = model('User/User');
        $userInfo = $userMdeol->getUserByUnionId($openUnionInfo['unionId']);
        return $userInfo;
    }

    /**
     * 注册
     * @param type $openId
     * @param type $type
     * @param type $unionId
     * @param type $nickname
     * @param type $img
     * @param type $city
     * @param type $province
     * @param type $country
     * @param type $sex
     */
    public function register($openId, $type, $unionId, $nickname, $img, $city, $province, $country, $sex) {
        $this->db->begin();
        $openUnionModel = model('User/OpenUnion');
        $openUnionInfo = $openUnionModel->getDataByOpenId($openId, $type);
        if (empty($openUnionInfo)) {
            $openUnionModel->setUnionId($unionId);
            $openUnionModel->setOpenId($openId);
            $openUnionModel->setType($type);
            $openUnionModel->setCreateTime(time());
            $result = $openUnionModel->save();
            if ($result == FALSE) {
                $this->db->rollback();
                $messages = $openUnionModel->getMessages();
                foreach ($messages as $message) {
                    E($message, "\n");
                }
            }
        }
        $userMdeol = model('User/User');
        $userInfoObject = $userMdeol->getUserByUnionId($openUnionInfo['unionId'], "", TRUE);
        if (empty($openUnionInfo)) {
            $userMdeol->setUnionId($unionId);
            $userMdeol->setNickname($nickname);
            $userMdeol->setImg($img);
            $userMdeol->setMinibegMoney(0);
            $userMdeol->setMinisendMoney(0);
            $userMdeol->setMinisendCount(0);
            $userMdeol->setMinibegCount(0);
            $userMdeol->setMoney(0);
            $userMdeol->setCity($city);
            $userMdeol->setProvince($province);
            $userMdeol->setCountry($country);
            $userMdeol->setSex($sex);
            $userMdeol->setCreateTime(time());
            $result = $userMdeol->save();
            if ($result == FALSE) {
                $this->db->rollback();
                $messages = $userMdeol->getMessages();
                foreach ($messages as $message) {
                    E($message, "\n");
                }
            }
            $userInfo = $userMdeol->toArray();
        } else {
            $userInfoObject->setNickname($nickname);
            $userInfoObject->setImg($img);
            $userInfoObject->setProvince($province);
            $userInfoObject->setCountry($country);
            $userInfoObject->setSex($sex);
            $result = $userInfoObject->save();
            if ($result == FALSE) {
                $this->db->rollback();
                $messages = $userInfoObject->getMessages();
                foreach ($messages as $message) {
                    E($message, "\n");
                }
            }
            $userInfo = $userInfoObject->toArray();
        }
        $this->db->commit();
        return $userInfo;
    }

    public function getUserInfoByUserId($id) {
        $id = intval($id) ? intval($id) : E("用户id不能为空");
        $userMdeol = model('User/User');
        $userInfo = $userMdeol->getDataById($id);
        if ($userInfo) {
            $openUnionMdeol = model('User/OpenUnion');
            $condition['unionId'] = $userInfo['unionId'];
            $openUnionList = $openUnionMdeol->getLists($condition);
            foreach ($openUnionList as $key => $value) {
                $userInfo['openId' . $value['type']] = $value['openId'];
            }
        }
        return $userInfo;
    }

    public function saveQmzfOpenId($userId, $qmzfOpenId) {
        $userModel = model("User/User");
        $userInfoObject = $userModel->getDataById($userId, $columns = '', TRUE);
        $userInfoObject->setQmzfOpenId($qmzfOpenId);
        $result = $userInfoObject->save();
        if ($result === FALSE) {
            E("操作订单数据出错");
        }
    }

}

?>
